//
//  LHVisitorView.swift
//  SinaWeibo
//
//  Created by 美食家 on 17/4/1.
//  Copyright © 2017年 不孤独的美食家. All rights reserved.
//

import UIKit

class LHVisitorView: UIView {

    var visitorInfo:[String:String]?{
        didSet{
        
            //print("visitorInfo=\(visitorInfo)")
            guard let imageName=visitorInfo?["imageName"] ,
                let message=visitorInfo?["message"] else {
                    //print("字典取值失败")
                    return
            }
            
            tipLabel.text=message
            
            if  imageName == ""{
                startAnimation()
                return
            }
            
            iconImage.image=UIImage(named: imageName)
            houseIconImage.isHidden=true
            maskIconImage.isHidden=true
        }
    
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /** 首页旋转动画 */
    private func startAnimation() -> Void {
       
        let anin=CABasicAnimation(keyPath: "transform.rotation")
        anin.toValue=2*M_PI
        anin.repeatCount=MAXFLOAT
        anin.duration=5
        anin.isRemovedOnCompletion=false
        iconImage.layer.add(anin, forKey: nil)
    }
    
    
    // MARK: - 本类私有控件
    /** 转动图像 */
    var iconImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /** 遮罩图像 */
    var maskIconImage = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /** 小房子图像 */
    var houseIconImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /** 提示标签 */
    var tipLabel : UILabel = UILabel.cz_label(withText: "关注一些人，发现有趣的事,做自己想做的事", fontSize: 16, color: UIColor.darkGray)
    /** 注册按钮 */
    var registerBtn : UIButton = UIButton.cz_textButton("注册",
                                                        fontSize: 16,
                                                        normalColor: UIColor.orange,
                                                        highlightedColor: UIColor.orange,
                                                        backgroundImageName: "common_button_white_disable")
    /** 登录按钮 */
    var loginBtn    : UIButton = UIButton.cz_textButton("登陆",
                                                        fontSize: 16,
                                                        normalColor: UIColor.orange,
                                                        highlightedColor: UIColor.red,
                                                        backgroundImageName: "common_button_white_disable")
}

extension LHVisitorView{

    func setupUI() -> Void {
        backgroundColor=UIColor.cz_color(withHex: 0xEDEDED)
        /** 1.取消视图的autoresizing */
        addSubview(iconImage)
        addSubview(maskIconImage)
        addSubview(houseIconImage)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        tipLabel.textAlignment = .center
        
        /** 2.取消视图的autoresizing */
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints=false
        }
        
        /** 3.自动布局 */
        addConstraint(NSLayoutConstraint(item: iconImage,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImage,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        addConstraint(NSLayoutConstraint(item: houseIconImage,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImage,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIconImage,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconImage,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconImage,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20.0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20.0))
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20.0))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerBtn,
                                         attribute: .width,
                                         multiplier: 1.0,
                                         constant: 0))
        let viewDict = ["maskIconImage":maskIconImage,"registerBtn":registerBtn] as [String : Any]
        let metrics  = ["spacing":-20]
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconImage]-0-|",
                                                      options: [],
                                                      metrics: nil,
                                                      views: viewDict))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconImage]-(spacing)-[registerBtn]",
                                                      options: [],
                                                      metrics: metrics,
                                                      views: viewDict))
        

    }
}


